接入Cloudflare R2

access cloudflare r2 using aws cli

  1. Create R2 tokens from the r2 Overview page. “Manage r2 API Tokens” -> “Create API Token”

  2. Copy the access key id and secret access key values

  3. Copy the endpoint url. It’s of the form <cloudflare_account_id>.r2.cloudflarestorage.com. Cloudflare account id is present in the url bar too when you are accessing the cloudflare dashboard and is also displayed in overview page of r2

  4. ~/.aws/config:

    [profile cloudflare]
    endpoint_url = https://<cloudlare_account_id>.r2.cloudflarestorage.com
    
  5. ~/.aws/credentials

    [cloudflare]
    aws_access_key_id = <> 
    aws_secret_access_key = <>
    
  6. test with:

    #非管理员权限令牌可能会被access deny
    aws s3 ls
    

命令行操作

C:\Users\86153>aws configure --profile cloudflare
AWS Access Key ID [None]: ******
AWS Secret Access Key [None]: ***
Default region name [None]: auto
Default output format [None]: json

遇到问题

1. 上传文件时报错unsupported trailed: x-amz-checksum-crc64nvme not implemented

原因:Cloudflare R2 当前不支持x-amz-checksum-crc64nvme算法。

解决方法:--checksum-algorithm CRC32或者禁用校验

[default]
s3 =
    use_crc64_checksum = false

或者Avoiding x-amz-checksum-mode Errors on Alternative S3 backends | Jonathan Bennett

AWS_REQUEST_CHECKSUM_CALCULATION = WHEN_REQUIRED
AWS_RESPONSE_CHECKSUM_VALIDATION = WHEN_REQUIRED

2. 上传文件时在某些节点卡住

原因:AWS CLI 默认对大于 5MB 的文件启用分段上传。如果恰好卡在5.0MB,可能是Cloudflare R2 的分段上传实现与 AWS S3 可能存在兼容性问题。

解决办法:设置--no-multipart参数禁用上传,或者弄一个很大的值

[default]
s3 =
    multipart_threshold = 5GB